/***********************************************************************************************************************
/*
/*    utility.cpp
/*
/**********************************************************************************************************************/

#include "stdafx.h"
#include <shlobj.h>


//-------------------------------------------------------------------------------------------------------
//   SET DIALOG ICON
//-------------------------------------------------------------------------------------------------------
void set_dialog_icon ( HWND hwnd, int iID )
{
	SendMessage ( hwnd, WM_SETICON, TRUE,  (LPARAM) LoadIcon((HINSTANCE) GetWindowLongPtr ( hwnd, GWLP_HINSTANCE ), MAKEINTRESOURCE ( iID )));
	SendMessage ( hwnd, WM_SETICON, FALSE, (LPARAM) LoadIcon((HINSTANCE) GetWindowLongPtr ( hwnd, GWLP_HINSTANCE ), MAKEINTRESOURCE ( iID )));
}

//-------------------------------------------------------------------------------------------------------
//   CENTER WINDOW AROUND RECT
//-------------------------------------------------------------------------------------------------------
void center_window_around_rect ( HWND h, RECT * r )
{

	POINT c; // center

	c.x = ( r->right + r->left ) / 2;
	c.y = ( r->bottom + r->top ) / 2;

	RECT w;
	GetWindowRect ( h, &w );
	int iDimX = w.right - w.left;
	int iDimY = w.bottom - w.top;

	SetWindowPos ( h, NULL, c.x - iDimX / 2, c.y - iDimY / 2, 0, 0, SWP_NOSIZE );
}

//-------------------------------------------------------------------------------------------------------
//   GET LOCAL APP DATA DIRECTORY
//-------------------------------------------------------------------------------------------------------
const wchar_t * get_local_app_data_directory ( cStrW * s )
{
	wchar_t szPath [ MAX_PATH ];

	if ( SUCCEEDED ( SHGetFolderPath ( NULL, 
								       CSIDL_LOCAL_APPDATA, 
									   NULL, 
									   0, 
									   szPath ) ) ) 
	{
		*s = szPath;
		*s += L'\\';
		return s->cstr();
	}

	return NULL;
}

//-------------------------------------------------------------------------------------------------------
//   GET OUR LOCAL APP DATA DIRECTORY
//   creates it if it doesn't exist
//-------------------------------------------------------------------------------------------------------
const wchar_t * get_our_local_app_data_directory ( cStrW * pRet, const wchar_t * pAppTitle )
{
	cStrW s;

	if ( ! get_local_app_data_directory ( &s) )
		return NULL;

	s += pAppTitle;

	if ( ! CreateDirectoryW ( s.cstr(), NULL ) ) // zero is error
	{
		if ( ERROR_ALREADY_EXISTS != GetLastError() )
			return NULL;
	}

	*pRet = s;
	*pRet += L"\\";
	return pRet->cstr();
}